<?php 

function local_news_search($queryarray, $andor, $limit, $offset, $userid){
	global $xoopsDB, $xoopsModule, $xoopsConfig, $xoopsModuleConfig;
	
	
	$sql = "SELECT ID, headline, byline, dateline, publish_date FROM " . $xoopsDB -> prefix("local_news_stories") . "
	WHERE ";
	
			if (is_array($queryarray) && $count = count($queryarray))
		{
			$sql .= " ((headline LIKE '%$queryarray[0]%' OR tease LIKE '%$queryarray[0]%' OR byline LIKE '%$queryarray[0]%' OR dateline LIKE '%$queryarray[0]%')";
			for($i = 1;$i < $count;$i++)
			{
				$sql .= " $andor ";
				$sql .= "(headline LIKE '%$queryarray[$i]%' OR tease LIKE '%$queryarray[$i]%' OR byline LIKE '%$queryarray[$i]%' OR dateline LIKE '%$queryarray[$i]%')";
			}
			$sql .= ") ";
		}
		$sql .= "ORDER BY publish_date DESC";
		$result = $xoopsDB -> query($sql, 10);
		if($result){
			$ret = array();
			$i=0;
			while ($myrow = $xoopsDB->fetchArray($result) ) {
				$ret[$i]['link'] = XOOPS_URL."/modules/local_news/index.php?op=sideBlock&ID=".$myrow['ID'];
				$ret[$i]['title'] = $myrow['headline'];
				$ret[$i]['byline'] = $myrow['byline'];
				$ret[$i]['dateline'] = $myrow['dateline'];
				$ret[$i]['publish_date'] = date("m/d/Y",$myrow['publish_date']);	
				++$i;				
			}//end loop
		}

		
		
//############################
//Include items from regional DB in search results

		  $modhandler = &xoops_gethandler('module');
		  $module = &$modhandler->getByDirname("local_news");
		  $config_handler = &xoops_gethandler('config');
		  $xoopsModuleConfig = &$config_handler->getConfigsByCat(0,$module->getVar('mid')); 
		  $enableSyndication = $xoopsModuleConfig['enableSyndication'];	
		  if($enableSyndication == 1){
					  	$sql = "SELECT ID, headline, byline, dateline, publish_date FROM CA_SE_News 
						WHERE storyID NOT LIKE '".strtoupper($xoopsModuleConfig['stationLetters'])."%' AND ";
				
						if (is_array($queryarray) && $count = count($queryarray))
					{
						$sql .= " ((headline LIKE '%$queryarray[0]%' OR tease LIKE '%$queryarray[0]%' OR byline LIKE '%$queryarray[0]%' OR dateline LIKE '%$queryarray[0]%')";
						for($i = 1;$i < $count;$i++)
						{
							$sql .= " $andor ";
							$sql .= "(headline LIKE '%$queryarray[$i]%' OR tease LIKE '%$queryarray[$i]%' OR byline LIKE '%$queryarray[$i]%' OR dateline LIKE '%$queryarray[$i]%')";
						}
						$sql .= ") ";
					}
					$sql .= "ORDER BY publish_date DESC";
						
					$link = mysql_connect($xoopsModuleConfig['syndicationDB_Host'], $xoopsModuleConfig['syndicationDB_User'], $xoopsModuleConfig['syndicationDB_Passaword']) or ($error = mysql_error());
					mysql_select_db($xoopsModuleConfig['syndicationDB']) or ($error = "Could not select database");
				    $result = mysql_query ($sql, $link) or ($error = "Query failed");
				    	
					if($result){
						--$i;
						while ($myrow = mysql_fetch_array($result, MYSQL_ASSOC)) {
							$ret[$i]['link'] = XOOPS_URL."/modules/local_news/index.php?op=sideBlock&syndicated=true&ID=".$myrow['ID'];
							$ret[$i]['title'] = $myrow['headline'];
							$ret[$i]['byline'] = $myrow['byline'];
							$ret[$i]['dateline'] = $myrow['dateline'];
							$ret[$i]['publish_date'] = date("m/d/Y",$myrow['publish_date']);	
							++$i;				
						}//end loop
					}
					
					mysql_free_result($result);
					mysql_close ($link);
					return $ret;
		  }
		
	



}//end function

?>
